package com.ray.project.rssreader;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.ray.project.gpslocation.GpsLocationHelper;
import com.ray.project.ndbc.OceanicBuoyDataGetter;
import com.ray.project.oceanicbuoy.OceanicBuoy;
import com.ray.project.oceanicwaveinformation.*;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class rssreader extends ListActivity {
	
    private ArrayList<OceanicBuoy> buoyDataList;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.rssreader);          
    }    
    
    @Override
    public void onStart() {
    	super.onStart();
    	GeoPoint lastKnownGeoPoint = getLastKnownGeoPoint();    	
        createList(lastKnownGeoPoint);
    }
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent viewMessage = new Intent(Intent.ACTION_VIEW, 
				Uri.parse(buoyDataList.get(position).getLink()));
		this.startActivity(viewMessage);
	}

	private void createList(final GeoPoint point) {
    	try{
    		// Get the radius from the Android Preferences
    	    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    		final String radius = prefs.getString("PREF_BUOY_RADIUS", "defValue");
    		   
    		String lats = GpsLocationHelper.parseGeoPoint(point.getLatitudeE6() / GpsLocationHelper.MULTIPLIER, true);
    		String lons = GpsLocationHelper.parseGeoPoint(point.getLongitudeE6() / GpsLocationHelper.MULTIPLIER, false);                       
            
    		OceanicBuoyDataGetter getter = new OceanicBuoyDataGetter(lats, lons, radius);                
            buoyDataList = getter.getBuoyData();                          
                      
    		List<String> titles = new ArrayList<String>(buoyDataList.size());    		 		
    		
    		for (OceanicBuoy ob : buoyDataList) {    			
    			titles.add(ob.getTitle() + " - " + ob.getDescription());	
    		}
	    	ArrayAdapter<String> adapter = 
	    		new ArrayAdapter<String>(this, R.layout.row,titles);
	    	this.setListAdapter(adapter);
    	} catch (Throwable t){
    		Log.e("Error: ",t.getMessage(),t);
    	}
    }
    
	  private GeoPoint getLastKnownGeoPoint() {
	        GeoPoint lastKnownLocationPoint = null;   
	        Location lastKnownLocation;   
	        LocationManager lm = (LocationManager) this.getSystemService(rssreader.LOCATION_SERVICE);   
	        
	        SharedPreferences useGps = PreferenceManager.getDefaultSharedPreferences(this);
	 	    Boolean useGpsTest = useGps.getBoolean("PREF_USE_GPS", true); 	   
	 	    
	 	    if (useGpsTest==true) {
	 	    	lastKnownLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	 	    	if (lastKnownLocation == null) {
	 	    		lastKnownLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	 	    	}
	 	    	if (lastKnownLocation != null) {
	 	            lastKnownLocationPoint = GpsLocationHelper.getGeoPoint(lastKnownLocation);
	 	    	}
	 	    	else {
	 	    		lastKnownLocationPoint = GpsLocationHelper.HONOLULU;
	 	    		Toast.makeText(this,
	 	    				"Application cannot determine your current location - setting location to Honoluly, Hawaii. Select fixed locations from the Settings screen.",
	 	    				Toast.LENGTH_LONG).show();
	 	    	} 	    		
	 	    }
	 	    else {
	 	    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
	 	 	    final String pref = prefs.getString("PREF_DEFAULT_LOCATION", "defValue");
	 	 	    
	 	 	    String latitude = pref.substring(0, pref.indexOf(":"));
	 	 	    String longitude = pref.substring(pref.indexOf(":")+1);
	 	 	    
	 	 	    lastKnownLocationPoint = GpsLocationHelper.getPreferenceGeoPoint(latitude, longitude);
	 	    }   
	      
	        return lastKnownLocationPoint;
	    }
	
}